package com.dayuanit.hotal;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Restaurant {

    private String name;

    //保存用户的订单明细
    private Map<String, List<OrderDetail>> orderMap = new HashMap<>();

    //菜单 原始数据
    private static final Map<Integer, Food> menuMap = new HashMap<>();
    static {
        init();
    }


    //初始化菜单
    private static void init() {
        menuMap.put(1000, new Food(1000, "宫保鸡丁", 20));
        menuMap.put(1001, new Food(1001, "米饭", 2));
        menuMap.put(1002, new Food(1002, "蛋炒饭", 12));
    }

    public Restaurant(String name) {
        this.name = name;
    }

    //点餐
    public void orderFood(Person person, OrderDetail orderDetail) {


        //用户已经点过一次
        if (orderMap.containsKey(person.getName())) {
            List<OrderDetail> orderDetails = orderMap.get(person.getName());
            orderDetails.add(orderDetail);

            //取出来的对象，改掉了，需要再put一次吗？
//            orderMap.put(person.getName(), orderDetails);

        } else {
            //用户第一次点餐
            List<OrderDetail> orderDetails = new ArrayList<>();
            orderDetails.add(orderDetail);
            orderMap.put(person.getName(), orderDetails);
        }
    }

    public int countPerson() {
        return orderMap.size();
    }

    public List<OrderDetail> getPersonDetail(Person person) {
        List<OrderDetail> orderDetails = orderMap.get(person.getName());
        return orderDetails;
    }

    //结算
    public void settlement(Person person) {
        List<OrderDetail> orderDetails = orderMap.get(person.getName());
        double orderAmount = 0;
        for (OrderDetail detail : orderDetails) {
            int count = detail.getCount();
            int foodId = detail.getFoodId();

            //通过foodID,找到Food对象
            Food food = menuMap.get(foodId);
            double totalPrice = food.getPrice() * count;
            orderAmount += totalPrice;

            detail.setPrice(food.getPrice());//对Food做一次快照，也就是拷贝其属性，防止以后food价格变掉，影响订单；订单一旦创建就不会改变。
            detail.setFoodName(food.getName());
            detail.setTotalAmount(totalPrice);
        }

        //实际支付的金额
        double payAmount = discount(orderAmount);
        System.out.println(person.getName() + "订单总价格：" + orderAmount + "，支付金额：" + payAmount);

        //支付
        pay(person, payAmount);
        System.out.println(person.getName() + "余额：" + person.getBalance());

        //优惠金额
        double youhuiAmount = orderAmount - payAmount;

        //打印小票
        TicktUtil.print(this.name, orderAmount, payAmount, youhuiAmount, orderDetails);
//        TicktUtil.print(this, orderAmount, payAmount, youhuiAmount, orderDetails);
    }

    /**
     * 支付 买单
     * @param person
     * @param payAmount
     */
    private void pay(Person person, double payAmount) {
        double balance = person.getBalance();
        if (balance < payAmount) {
            throw new PayException("余额不足，请充值!");
        }

        balance -= payAmount;
        person.setBalance(balance);
    }


    /**
     * 处理打折
     * @param orderAmount
     */
    private double discount(double orderAmount) {
//        2)消费在100元以上者，优惠10元。
//        3)消费在200元以上者，优惠30元。
//        4)消费在500元以上者，打8折。

        if (orderAmount >= 500) {
            return orderAmount * 0.8;
        }
        if (orderAmount < 500 && orderAmount >= 200) {
            return orderAmount - 30;
        }
        if (orderAmount >= 100 && orderAmount < 200) {
            return orderAmount - 10;
        }

        return orderAmount;
    }


    public String getName() {
        return name;
    }
}
